# coding: utf-8
import ui
import os
import datetime
from operator import itemgetter


class MyTableViewDataSource(object):
    def __init__(self, row_height):
        self.row_height = row_height
        self.width = None

    def tableview_number_of_rows(self, tableview, section):
        return len(tableview.data_source.items)

    def tableview_cell_for_row(self, tableview, section, row):
        self.width, height = ui.get_screen_size()
        cell = ui.TableViewCell()
        cell.bounds = (0, 0, self.width, self.row_height)
        for i in range(3):
            self.make_labels(cell, tableview.data_source.items[row][i], i)
        return cell

    def make_labels(self, cell, text, pos):
        label = ui.Label()
        label.border_color = "lightgrey"
        label.border_width = 0.5
        if pos == 2:
            label.text = str(datetime.datetime.fromtimestamp(text))
        else:
            label.text = str(text)
        label.frame = (pos * self.width / 3, 0, self.width / 3, self.row_height)
        label.alignment = ui.ALIGN_CENTER
        cell.content_view.add_subview(label)


class MyTableView(ui.View):
    def __init__(self):
        self.dirs = []
        self.files = []
        self.order = 'asc'
        self.active_button = None
        self.button_height = 50
        self.btn_name = self.make_buttons("Name")
        self.btn_size = self.make_buttons("Size")
        self.btn_date = self.make_buttons("Date")
        self.tv = ui.TableView()
        self.tv.row_height = 30
        self.tv.data_source = MyTableViewDataSource(self.tv.row_height)
        self.get_dir()
        self.all_items = self.dirs + self.files
        self.tv.data_source.items = self.all_items
        self.name = "TableView-Test"
        self.tv.allows_selection = False
        self.add_subview(self.tv)
        self.present("fullscreen")

    def make_buttons(self, name):
        button = ui.Button()
        button.name = name
        button.title = name
        button.border_color = 'blue'
        button.border_width = 1
        button.corner_radius = 3
        button.background_color = 'white'
        button.action = self.btn_action
        self.add_subview(button)
        return button

    def btn_action(self, sender):
        names = [self.btn_name.name, self.btn_size.name, self.btn_date.name]  # ['Name', 'Size', 'Date']
        sender_index = names.index(sender.name)  # 0/1/2

        if self.order == 'asc':
            self.order = 'desc'
            self.all_items = sorted( self.all_items, key=itemgetter(sender_index))
        else:
            self.order = 'asc'
            self.all_items = sorted( self.all_items, key=itemgetter(sender_index), reverse=True )

        self.tv.data_source.items = self.all_items
        self.tv.reload()

    def layout(self):
        self.tv.reload()
        self.btn_name.frame = (0 * self.width / 3, 0, self.width / 3, self.button_height, )
        self.btn_size.frame = (1 * self.width / 3, 0, self.width / 3, self.button_height, )
        self.btn_date.frame = (2 * self.width / 3, 0, self.width / 3, self.button_height, )
        self.tv.frame = (0, self.button_height, self.width, self.height - self.button_height, )

    def get_dir(self):
        path = os.getcwd()
        if path == os.path.expanduser("~"):
            self.dirs = []
        else:
            self.dirs = [["..", 0, 0.0]]
        self.files = []
        for entry in sorted(os.listdir(path)):
            full_pathname = path + "/" + entry
            if os.path.isdir(full_pathname):
                date = os.path.getmtime(full_pathname)
                self.dirs.append((entry, "<DIR>", date))
            else:
                size = os.path.getsize(full_pathname)
                date = os.path.getmtime(full_pathname)
                self.files.append((entry, size, date))


MyTableView()
